package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.constant.XxConstant;
import com.ruoyi.system.domain.XxScheduleUserSection;
import com.ruoyi.system.domain.XxSectionMonthBuildPlan;
import com.ruoyi.system.domain.XxSectionMonthBuildPlanProcess;
import com.ruoyi.system.mapper.XxScheduleUserSectionMapper;
import com.ruoyi.system.mapper.XxSectionMonthBuildPlanMapper;
import com.ruoyi.system.mapper.XxSectionMonthBuildPlanProcessMapper;
import com.ruoyi.system.service.IXxScheduleUserSectionService;
import com.ruoyi.system.service.XxPlanService;
import com.ruoyi.system.vo.XxPlanBuildChartResponse;
import com.ruoyi.system.vo.XxPlanBuildRequest;
import com.ruoyi.system.vo.XxPlanBuildResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author LiBingyan
 * @Date 2022/4/19 15:14
 * @Version 1.0
 **/
@Service
public class XxPlanServiceImpl implements XxPlanService {
    @Autowired
    XxSectionMonthBuildPlanMapper xxSectionMonthBuildPlanMapper;
    @Autowired
    XxSectionMonthBuildPlanProcessMapper xxSectionMonthBuildPlanProcessMapper;
    @Autowired
    XxScheduleUserSectionMapper xxScheduleUserSectionMapper;
    @Autowired
    IXxScheduleUserSectionService iXxScheduleUserSectionService;

    /**
     * 月计划进度查询
     *
     * @param xxPlanBuildRequest
     * @return
     */
    @Override
    public XxPlanBuildResponse getInfo(XxPlanBuildRequest xxPlanBuildRequest) {
        xxPlanBuildRequest = checkXxPlanBuildRequest(xxPlanBuildRequest);
        XxPlanBuildResponse xxPlanBuildResponse = new XxPlanBuildResponse();
        SysUser sysUser = ShiroUtils.getSysUser();
        if (Objects.equals(sysUser, null)) {
            return null;
        }
        List<XxSectionMonthBuildPlan> xxSectionMonthBuildPlans = new ArrayList<>();
        List<XxSectionMonthBuildPlanProcess> xxSectionMonthBuildPlanProcesses = new ArrayList<>();
        Set<Date> dateList = new TreeSet<>();
        if (Objects.equals(xxPlanBuildRequest.getQueryType(), XxConstant.XX_YJH)) {
            LambdaQueryWrapper<XxSectionMonthBuildPlan> xxSectionMonthBuildPlanLambdaQueryWrapper = new LambdaQueryWrapper<>();
            xxSectionMonthBuildPlanLambdaQueryWrapper.le(XxSectionMonthBuildPlan::getBuildTime, xxPlanBuildRequest.getEndTime()).ge(XxSectionMonthBuildPlan::getBuildTime, xxPlanBuildRequest.getStartTime())
                    .eq(XxSectionMonthBuildPlan::getSectionName, xxPlanBuildRequest.getSection());
            xxSectionMonthBuildPlans = xxSectionMonthBuildPlanMapper.selectList(xxSectionMonthBuildPlanLambdaQueryWrapper);
            xxPlanBuildResponse.setMonthBuildPlan(xxSectionMonthBuildPlans);
            for (XxSectionMonthBuildPlan xxSectionMonthBuildPlan : xxSectionMonthBuildPlans) {
                String s = DateUtil.formatDate(xxSectionMonthBuildPlan.getBuildTime());
                Date date = DateUtil.parse(s, "yyyy-MM");
                dateList.add(date);
            }
        } else {
            LambdaQueryWrapper<XxSectionMonthBuildPlanProcess> xxSectionMonthBuildPlanProcessLambdaQueryWrapper = new LambdaQueryWrapper<>();
            xxSectionMonthBuildPlanProcessLambdaQueryWrapper.le(XxSectionMonthBuildPlanProcess::getBuildTime, xxPlanBuildRequest.getEndTime()).ge(XxSectionMonthBuildPlanProcess::getBuildTime, xxPlanBuildRequest.getStartTime());
            xxSectionMonthBuildPlanProcesses = xxSectionMonthBuildPlanProcessMapper.selectList(xxSectionMonthBuildPlanProcessLambdaQueryWrapper);
            xxPlanBuildResponse.setMonthBuildPlanProcess(xxSectionMonthBuildPlanProcesses);
            for (XxSectionMonthBuildPlanProcess xxSectionMonthBuildPlanProcess : xxSectionMonthBuildPlanProcesses) {
                String s = DateUtil.formatDate(xxSectionMonthBuildPlanProcess.getBuildTime());
                Date date = DateUtil.parse(s, "yyyy-MM");
                dateList.add(date);
            }
        }
        if (!sysUser.isAdmin()) {
            xxPlanBuildResponse.setMonthBuildPlanProcess(new ArrayList<>());
            String loginName = sysUser.getLoginName();
            List<XxSectionMonthBuildPlan> collect = xxSectionMonthBuildPlans.stream().filter(xxSectionMonthBuildPlan -> Objects.equals(loginName, xxSectionMonthBuildPlan.getCreateBy())).collect(Collectors.toList());
            xxPlanBuildResponse.setMonthBuildPlan(collect);
        }
        xxPlanBuildResponse.setDateList(new ArrayList<>(dateList));
        Boolean scheduleUserSectionByUserName = iXxScheduleUserSectionService.getScheduleUserSectionByUserName(sysUser.getLoginName(), xxPlanBuildRequest.getSection());
        xxPlanBuildResponse.setRight(scheduleUserSectionByUserName);
        List<BigDecimal> dataList = new ArrayList<>();
        dataList = getTrueList(dataList, xxPlanBuildRequest, xxSectionMonthBuildPlans);
        List<XxPlanBuildChartResponse> xxPlanBuildChartResponseList = new ArrayList<>();
        XxPlanBuildChartResponse xxPlanBuildChartResponse = new XxPlanBuildChartResponse();
        xxPlanBuildChartResponse.setData(dataList);
        xxPlanBuildChartResponse.setName(xxPlanBuildRequest.getQueryParam());
        xxPlanBuildChartResponseList.add(xxPlanBuildChartResponse);
        xxPlanBuildResponse.setChartList(xxPlanBuildChartResponseList);
        return xxPlanBuildResponse;
    }

    /**
     * 返回编制人列表
     * @return
     */
    @Override
    public List<String> getUser() {
        QueryWrapper<XxScheduleUserSection> xxScheduleUserSectionQueryWrapper = new QueryWrapper<>();
        xxScheduleUserSectionQueryWrapper.select("DISTINCT userName");
        List<String> stringList = new ArrayList<>();
        for (XxScheduleUserSection xxScheduleUserSection : xxScheduleUserSectionMapper.selectList(xxScheduleUserSectionQueryWrapper)) {
            stringList.add(xxScheduleUserSection.getUserName());
        }
        return stringList;
    }

    /**
     * 初始化参数不对的XxPlanBuildRequest
     *
     * @param xxPlanBuildRequest
     * @return
     */
    private XxPlanBuildRequest checkXxPlanBuildRequest(XxPlanBuildRequest xxPlanBuildRequest) {
        if (Objects.equals(xxPlanBuildRequest.getStartTime(), null)) {
            Date start = DateUtil.parse("2007-03-01");
            xxPlanBuildRequest.setStartTime(start);
        }
        if (Objects.equals(xxPlanBuildRequest.getEndTime(), null)) {
            Date end = DateUtil.parse("2007-03-01");
            xxPlanBuildRequest.setEndTime(end);
        }
        if (Objects.equals(xxPlanBuildRequest.getCreateBy(), null)) {
            xxPlanBuildRequest.setCreateBy("");
        }
        if (Objects.equals(xxPlanBuildRequest.getSection(), null)) {
            xxPlanBuildRequest.setSection(XxConstant.TJ01);
        }
        if (Objects.equals(xxPlanBuildRequest.getQueryType(), null)) {
            xxPlanBuildRequest.setQueryType(XxConstant.XX_YJH);
        }
        return xxPlanBuildRequest;
    }

    /**
     * 根据查询参数获取正确的值
     *
     * @return
     */
    private List<BigDecimal> getTrueList(List<BigDecimal> dataList, XxPlanBuildRequest xxPlanBuildRequest, List<XxSectionMonthBuildPlan> xxSectionMonthBuildPlans) {
        for (XxSectionMonthBuildPlan xxSectionMonthBuildPlan : xxSectionMonthBuildPlans) {
            switch (xxPlanBuildRequest.getQueryParam()) {
                case XxConstant.LUJITIANFANG:
                    dataList.add(xxSectionMonthBuildPlan.getRoadbedFillingCount());
                    break;
                case XxConstant.LUJIWAFANG:
                    dataList.add(xxSectionMonthBuildPlan.getRoadbedCutCount());
                    break;
                case XxConstant.RUANJIHUANTIAN:
                    dataList.add(xxSectionMonthBuildPlan.getSoftFoundationFillingCount());
                    break;
                case XxConstant.SHUIWENJICENG:
                    dataList.add(xxSectionMonthBuildPlan.getWaterStabCount());
                    break;
                case XxConstant.LIQINGMIANCENG:
                    dataList.add(xxSectionMonthBuildPlan.getAsphaltCount());
                    break;
                case XxConstant.QIAOLIANGZHUANGJI:
                    dataList.add(xxSectionMonthBuildPlan.getBridgeFoundationCount());
                    break;
                case XxConstant.QIAOLIANGDUNZHU:
                    dataList.add(xxSectionMonthBuildPlan.getBridgePierCount());
                    break;
                case XxConstant.QIAOLIANGXILIANG:
                    dataList.add(xxSectionMonthBuildPlan.getBridgeBinderCount());
                    break;
                case XxConstant.QIAOLIANGGAILIANG:
                    dataList.add(xxSectionMonthBuildPlan.getBridgeBentCapCount());
                    break;
                case XxConstant.LIANGBANYUZHI:
                    dataList.add(xxSectionMonthBuildPlan.getPrefabricationBeamSlabCount());
                    break;
                case XxConstant.HANDONGJICHU:
                    dataList.add(xxSectionMonthBuildPlan.getCulvertBaseCount());
                    break;
                case XxConstant.HANDONGQIANGSHEN:
                    dataList.add(xxSectionMonthBuildPlan.getCulvertPenetrationOpeningCount());
                    break;
                case XxConstant.HANDONGGAIBAN:
                    dataList.add(xxSectionMonthBuildPlan.getCulvertCoverBoardCount());
                    break;
                default:
                    break;
            }
        }
        return dataList;
    }
}
